package com.crm.common.utils.bean;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.crm.common.crm.common.CrmModel;
import com.crm.common.crm.constant.CrmCodeEnum;
import com.crm.common.crm.entity.PO.CrmCustomer;
import com.crm.common.crm.entity.PO.CrmCustomerData;
import com.crm.common.crm.entity.PO.CrmField;
import com.crm.common.crm.entity.VO.CrmModelFiledVO;
import com.crm.common.ka.core.exception.CrmException;
import lombok.extern.slf4j.Slf4j;

import java.beans.PropertyDescriptor;
import java.lang.reflect.*;
import java.util.*;

@Slf4j
public class BeanExtendUtils{

    private List<String> propers = new ArrayList<String>(Arrays.asList(new String[]{"value", "fieldId", "name", "fieldName"}));

    public Map<String, Object> extraMap;

    private Class mclass;

    private Class tclass;

    public List<Object> list = new ArrayList<>();

    public List<CrmModelFiledVO> voList = new ArrayList<>();



    public BeanExtendUtils(Class tclass,Class mclass) {
        this.extraMap = new HashMap<>();
        this.tclass = tclass;
        this.mclass = mclass;
    }

    public Object copyObj(List<CrmField> sourceFieldList, List<CrmField> targeFieldList, CrmModel sourceMap) {
        try{
            Object t =tclass.newInstance();
            for (CrmField sourceField : sourceFieldList) {
                for (CrmField targeField : targeFieldList) {
                    if (sourceField.getRelevant() != null && targeField.getFieldId().equals(sourceField.getRelevant())) {
                        if (targeField.getFieldType().equals(1)) {
                            extraMap.put(targeField.getFieldName(), sourceMap.get(StrUtil.toCamelCase(sourceField.getFieldName())));
                        }
                    }
                }
            }
            BeanUtil.fillBeanWithMap(extraMap, t, true);
            return t;
        }catch (Exception e){
            log.error("主表字段属性拷贝错误，error:"+e);
            throw new CrmException(CrmCodeEnum.CRM_CALL_DATA_QUERY_ERROR,"主表字段继承,属性拷贝");
        }

    }

    public List copyDataList(List<CrmField> sourceFieldList, List<CrmField> targeFieldList, CrmModel sourceMap)  {
        try{
            for (CrmField sourceField : sourceFieldList) {
                for (CrmField targeField : targeFieldList) {
                    if (sourceField.getRelevant() != null && targeField.getFieldId().equals(sourceField.getRelevant())) {
                        Object m = mclass.newInstance();
                        Field[] fields = mclass.getDeclaredFields();//获得属性
                        for (Field field : fields) {
                            if(propers.contains(field.getName())){
                                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), mclass);
                                Method getMethod = pd.getWriteMethod();//获得set方法
                                Object value = sourceMap.get(StrUtil.toCamelCase(sourceField.getFieldName()));
                                if(Optional.ofNullable(value).isPresent()){
                                    if(field.getType().equals(java.lang.String.class)){
                                        getMethod.invoke(m, value.toString());//执行set方法
                                    }else if(field.getType().equals(java.lang.Integer.class)){
                                        getMethod.invoke(m, Integer.valueOf(value.toString()));//执行set方法
                                    }
                                }
                            }
                        }
                        list.add(m);
                    }
                }
            }
            return list;
        }catch(Exception e){
            log.error("自定义字段属性拷贝错误，error:"+e);
            throw new CrmException(CrmCodeEnum.CRM_CALL_DATA_QUERY_ERROR,"自定义字段继承,属性拷贝");
        }
    }

    public List<CrmModelFiledVO> copyVoList(List<CrmField> sourceFieldList, List<CrmField> targeFieldList, CrmModel sourceMap)  {
        try{
            for (CrmField sourceField : sourceFieldList) {
                for (CrmField targeField : targeFieldList) {
                    if (sourceField.getRelevant() != null && targeField.getFieldId().equals(sourceField.getRelevant())) {
                        Object value = sourceMap.get(StrUtil.toCamelCase(sourceField.getFieldName()));
                        CrmModelFiledVO crmModelFiled = BeanUtil.copyProperties(targeField, CrmModelFiledVO.class);
                        crmModelFiled.setValue(value);
                        voList.add(crmModelFiled);
                    }
                }
            }
            return voList;
        }catch(Exception e){
            log.error("自定义字段属性拷贝错误，error:"+e);
            throw new CrmException(CrmCodeEnum.CRM_CALL_DATA_QUERY_ERROR,"自定义字段继承,属性拷贝");
        }
    }

    public static void main(String args[]) {
        CrmModel crmModel = new CrmModel();
        List<CrmField> sourceFieldList = new ArrayList<>();
        List<CrmField> targeFieldList = new ArrayList<>();
        BeanExtendUtils im = new BeanExtendUtils(CrmCustomer.class,CrmCustomerData.class);
        CrmCustomer crmCustomer = (CrmCustomer) im.copyObj(sourceFieldList,targeFieldList,crmModel);
        List<CrmCustomerData> customerDataList = im.copyDataList(sourceFieldList,targeFieldList,crmModel);
        System.out.println(JSONObject.toJSONString(crmCustomer));
        System.out.println(JSONObject.toJSONString(customerDataList));
    }
}
